package
{
	import flash.events.Event;
	import flash.media.Sound;
	import flash.media.SoundChannel;
	import flash.media.SoundTransform;
	import flash.utils.ByteArray;
	/**
	 *声音管理 
	 * @author Nestor
	 * 
	 */
	public class GameSound
	{
		[Embed(source="../media/sounds/bgGame.mp3")]
		public var BgSnd:Class;
		
		[Embed(source="../media/sounds/hit.mp3")]
		public var Hit:Class;
		
		[Embed(source="../media/sounds/eat.mp3")]
		public var Eat:Class;
		
		[Embed(source="../media/sounds/lose.mp3")]
		public var Lose:Class;

		private var channel:SoundChannel;

		private var bgSound:Sound;
		private var bgVol:Number=0.8;
		
		private static var _instance:GameSound = null;
		public function GameSound(p:Param)
		{
		}
		public static function getInstance():GameSound{
			return _instance ||= new GameSound(new Param());
		}
		/**
		 *初始化  
		 * 
		 */		
		public function init():void{
			bgSound = new BgSnd() as Sound;
			channel = bgSound.play();
			var stf:SoundTransform = new SoundTransform(bgVol); 
			channel.soundTransform = stf;
			channel.addEventListener(Event.SOUND_COMPLETE,bgComHandler);
		}
		
		
		protected function bgComHandler(event:Event):void
		{
			channel.removeEventListener(Event.SOUND_COMPLETE,bgComHandler);
			channel = bgSound.play();
			var stf:SoundTransform = new SoundTransform(bgVol); 
			channel.soundTransform = stf;
			channel.addEventListener(Event.SOUND_COMPLETE,bgComHandler);
		}
		/**
		 *设置背景音乐声音 
		 * @param vol
		 * 
		 */		
		public function setBgVol(vol:Number):void{
			this.bgVol = vol;
			var stf:SoundTransform = new SoundTransform(vol); 
			channel.soundTransform = stf;
		}
		/**
		 *播放碰撞 
		 * 
		 */		
		public function playHit():void{
			var hit:Sound = new Hit() as Sound;
			hit.play();
		}
		/**
		 *播放吃 
		 * 
		 */		
		public function playEat():void{
			var eat:Sound = new Eat() as Sound;
			eat.play();
		}
		/**
		 *播放输 
		 * 
		 */		
		public function playLose():void{
			var lose:Sound = new Lose() as Sound;
			lose.play();
		}
		
	}
}
class Param{}